package com.hks.quart.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hks.quart.entity.ScheduleJobBean;
import com.hks.quart.entity.ScheduleJobExample;
import com.hks.quart.mapper.ScheduleJobMapper;
import com.hks.quart.service.ScheduleJobService;
import com.hks.quart.utils.ScheduleUtil;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhanglq
 * @version 1.0
 * @date 2021/9/1 15:36
 * @info 定时器服务实现类
 */
@Service
public class ScheduleJobServiceImpl  extends ServiceImpl<ScheduleJobMapper, ScheduleJobBean> implements ScheduleJobService {

    @Resource
    private Scheduler scheduler ;
    @Resource
    private ScheduleJobMapper scheduleJobMapper ;

    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时器初始化
     */
    @PostConstruct
    public void init (){
        ScheduleJobExample example = new ScheduleJobExample() ;
        List<ScheduleJobBean> scheduleJobBeanList = scheduleJobMapper.selectByExample(example) ;
        for (ScheduleJobBean scheduleJobBean : scheduleJobBeanList) {
            CronTrigger cronTrigger = ScheduleUtil.getCronTrigger(scheduler,scheduleJobBean.getJobId()) ;
            if (cronTrigger == null){
                ScheduleUtil.createJob(scheduler,scheduleJobBean);
            } else {
                ScheduleUtil.updateJob(scheduler,scheduleJobBean);
            }
        }
    }


    @Override
    public ScheduleJobBean selectByPrimaryKey(Long jobId) {
        return scheduleJobMapper.selectByPrimaryKey(jobId);
    }

    @Override
    public List<ScheduleJobBean> selectByExample(ScheduleJobExample example) {
        return scheduleJobMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertJob(ScheduleJobBean record) {
        ScheduleUtil.createJob(scheduler,record);
        return scheduleJobMapper.insertJob(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(ScheduleJobBean record) {
        ScheduleUtil.updateJob(scheduler,record);
        return this.scheduleJobMapper.updateById(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pauseJob(Long jobId) {
        ScheduleJobBean scheduleJobBean = scheduleJobMapper.selectByPrimaryKey(jobId) ;
        ScheduleUtil.pauseJob(scheduler,jobId);
        scheduleJobBean.setStatus(1);
        scheduleJobMapper.updateByPrimaryKeySelective(scheduleJobBean) ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resumeJob(Long jobId) {
        ScheduleJobBean scheduleJobBean = scheduleJobMapper.selectByPrimaryKey(jobId) ;
        ScheduleUtil.resumeJob(scheduler,jobId);
        scheduleJobBean.setStatus(0);
        scheduleJobMapper.updateByPrimaryKeySelective(scheduleJobBean) ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(Long jobId) {
        ScheduleJobBean scheduleJobBean = scheduleJobMapper.selectByPrimaryKey(jobId) ;
        ScheduleUtil.run(scheduler,scheduleJobBean);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long jobId) {
        ScheduleUtil.deleteJob(scheduler, jobId);
        scheduleJobMapper.deleteByPrimaryKey(jobId) ;
    }

    @Override
    public void updateTimer(Long jobId) {
        ScheduleJobBean scheduleJobBean = this.scheduleJobMapper.selectByPrimaryKey(jobId);
        CronTrigger cronTrigger = ScheduleUtil.getCronTrigger(scheduler,jobId);
        if (cronTrigger == null){
            ScheduleUtil.createJob(scheduler,scheduleJobBean);
        } else {
            ScheduleUtil.updateJob(scheduler,scheduleJobBean);
        }
    }
}
